package com.thd.springboottest.redisconfig.controller;

import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * com.thd.springboottest.redisconfig.controller.RedisConfigController
 *
 * @author: wanglei62
 * @DATE: 2022/3/25 16:44
 **/
@Controller
@RequestMapping(value="/redisconfig")
public class RedisConfigController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    private Logger logger = LoggerFactory.getLogger(RedisConfigController.class);
    @ResponseBody
    @RequestMapping(value="set/{k}/{v}",method= RequestMethod.GET)
    // http://127.0.0.1:8899/thd/redisconfig/set/a/3
    public Object set(@PathVariable String k,@PathVariable String v){
        this.logger.info(" redis set {} = {}" ,k,v);
        this.redisTemplate.opsForValue().set(k,v);
        return v;
    }

    @ResponseBody
    @RequestMapping(value="get/{k}",method= RequestMethod.GET)
    // http://127.0.0.1:8899/thd/redisconfig/get/a
    public Object get(@PathVariable String k){
        this.logger.info(" redis get{} ",k);
       Object v = this.redisTemplate.opsForValue().get(k);
       return v;
    }


    @ResponseBody
    @RequestMapping(value="testRedissonSet/{k}/{v}",method= RequestMethod.GET)
    // http://127.0.0.1:8899/thd/redisconfig/testRedissonSet/a/3
    public Object testRedissonSet(@PathVariable String k,@PathVariable String v){
        this.logger.info(" testRedissonSet {} = {}" ,k,v);
        this.redissonClient.getBucket(k).set(v);
        return v;
    }

    @ResponseBody
    @RequestMapping(value="testRedissonGet/{k}",method= RequestMethod.GET)
    // http://127.0.0.1:8899/thd/redisconfig/testRedissonGet/a
    public Object testRedissonGet(@PathVariable String k){
        this.logger.info(" testRedissonGet {} " ,k);
        Object v = this.redissonClient.getBucket(k).get();
        return v;
    }


    // 等待5秒的锁
    @ResponseBody
    @RequestMapping(value="trylockWait",method= RequestMethod.GET)
    // http://127.0.0.1:8899/thd/redisconfig/trylockWait
    public Object trylockWait() throws Exception{
        this.logger.info(" trylockWait" );
        // 尝试加锁，最多等待5秒，上锁以后10秒自动解锁
        this.redissonClient.getLock("lock").tryLock(5,10, TimeUnit.SECONDS);
        System.out.println(" lock");
        Thread.sleep(4000);
        this.redissonClient.getLock("lock").unlock();
        System.out.println(" unlock ");

        return "SUCCESS";
    }


    // 如果有锁则直接返回
    @ResponseBody
    @RequestMapping(value="trylock",method= RequestMethod.GET)
    // http://127.0.0.1:8899/thd/redisconfig/trylock
    public Object trylock() throws Exception{
        this.logger.info(" trylock" );

        try{
            // 尝试加锁，最多等待1秒，上锁以后10秒自动解锁
            if(this.redissonClient.getLock("lock").tryLock()){
                System.out.println(" lock");
                Thread.sleep(4000);
                this.redissonClient.getLock("lock").unlock();
                System.out.println(" unlock ");
            }else{
                return "繁忙";
            }

        }catch(Exception e){
            e.printStackTrace();
            return "繁忙";
        }


        return "SUCCESS";
    }


}
